<template>
  <div class="crrMaterialScrapReport-add-update-component">
    <el-dialog
      v-if="modelValue.visible"
      v-model="modelValue.visible"
      :title="
        !modelValue.form.id
          ? `新增${modelValue.title}`
          : `修改${modelValue.title}`
      "
      width="550px"
    >
      <div style="padding-right: 50px; padding-top: 20px">
        <el-form
          :model="modelValue.form"
          :rules="modelValue.rules"
          ref="addEditRef"
          :label-width="100"
        >
          <el-form-item label="门店名称" prop="storeId">
            <el-select-v2
              style="width: 100%"
              v-model="modelValue.form.storeId"
              @change="changeStore"
              placeholder="请选择门店"
              filterable
              :options="modelValue.storeList"
              :props="{
                label: 'name',
                value: 'id',
              }"
            >
            </el-select-v2>
          </el-form-item>
          <el-form-item label="机器唯一识别码" prop="deviceId">
            <el-select-v2
              style="width: 100%"
              v-model="modelValue.form.deviceId"
              placeholder="请选择机器唯一识别码"
              filterable
              :options="deviceInfoList"
              :props="{
                label: 'name',
                value: 'id',
              }"
            >
            </el-select-v2>
          </el-form-item>

          <el-form-item label="物料编号" prop="materialId">
            <el-select-v2
              style="width: 100%"
              v-model="modelValue.form.materialId"
              placeholder="请选择物料编号"
              filterable
              :options="modelValue.materialConfigList"
              :props="{
                label: 'name',
                value: 'id',
              }"
            >
            </el-select-v2>
          </el-form-item>
          <el-form-item label="报废次数" prop="scrapCount">
            <el-input
              placeholder="请输入报废次数"
              v-model="modelValue.form.scrapCount"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="报废总量" prop="totalScrapQuantity">
            <el-input
              placeholder="请输入报废总量"
              v-model="modelValue.form.totalScrapQuantity"
              autocomplete="off"
            />
          </el-form-item>
        </el-form>
      </div>
      <template #footer v-if="!modelValue.form.disabled">
        <span class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="ensure"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import {
  addCrrMaterialScrapReport,
  updateCrrMaterialScrapReport,
} from '@/api/crr-material-scrap-report.api'
import {
  getDeviceInfoPagination,
  getDeviceInfoAllList,
} from '@/api/dms-device-info.api'

const props = defineProps(['modelValue'])
const addEditRef = ref<FormInstance>()

const deviceInfoList = ref([])
const changeStore = () => {
  //根据门店获取门店下的机器
  let formData = {
    storeId: props.modelValue.form.storeId,
  }
  getDeviceInfoAllList(formData)
    .then((res: any) => {
      deviceInfoList.value = res
    })
    .catch((err: any) => {
      console.log(err)
    })
}

/***
 * 确认弹框
 */
const ensure = () => {
  let form = props.modelValue.form
  let formData = {
    storeId: form.storeId,
    deviceId: form.deviceId,
    materialId: form.materialId,
    scrapCount: form.scrapCount,
    totalScrapQuantity: form.totalScrapQuantity,
  }

  if (!addEditRef.value) return
  addEditRef.value.validate((valid, fields) => {
    if (valid) {
      if (form.id) {
        updateCrrMaterialScrapReport({
          ...formData,
          id: form.id,
        })
          .then((res) => {
            cancel()
            ElMessage({
              message: props.modelValue.title + '修改成功',
              type: 'success',
            })
            if (props.modelValue.refreshDataList) {
              props.modelValue.refreshDataList()
            }
          })
          .catch((err) => {
            ElMessage({
              message: err.msg || err,
              type: 'error',
            })
          })
        return
      }

      addCrrMaterialScrapReport(formData)
        .then((res) => {
          cancel()
          ElMessage({
            message: props.modelValue.title + '新增成功',
            type: 'success',
          })
          if (props.modelValue.refreshDataList) {
            props.modelValue.refreshDataList()
          }
        })
        .catch((err) => {
          ElMessage({
            message: err.msg || err,
            type: 'error',
          })
        })
    }
  })
}

/***
 * 取消弹框
 */
const cancel = () => {
  props.modelValue.visible = false
}
</script>
<style lang="scss" scoped></style>
